package org.BooCrack.bookstore.pay.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.BooCrack.bookstore.common.pojo.BSResult;
import org.BooCrack.bookstore.common.utils.Response;
import org.BooCrack.bookstore.common.utils.ResponseResult;
import org.BooCrack.bookstore.model.entity.custom.Cart;
import org.BooCrack.bookstore.model.entity.BookInfo;
import org.BooCrack.bookstore.model.service.IBookInfoService;
import org.BooCrack.bookstore.model.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
//@RequestMapping("/cart")
@RequestMapping("/user/V2")
@Api(value = "v1", tags = "2.BooCrack书城购物车接口")
public class CartController {

    @Autowired
    private IBookInfoService bookInfoService;

    @Autowired
    private ICartService cartService;



    /**
     * 加入购物车
     *
     * @param bookId
     * @param request
     * @return
     */
    @PostMapping("/cart")
    @ApiOperation(value = "加入购物车", notes = "加入购物车")
    public ResponseResult addToCart(@RequestParam(value = "bookId",defaultValue = "0") int bookId,
                                    @RequestParam(required = false,defaultValue = "0") int buyNum,
                                    HttpServletRequest request) {

        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //根据要加入购物车的bookId查询bookInfo
        BookInfo bookInfo = bookInfoService.queryBookAvailable(bookId);

        if (bookInfo != null) {
            //这本书在数据库里
            BSResult bsResult = cartService.addToCart(bookInfo, cart, buyNum);
            request.getSession().setAttribute("cart", bsResult.getData());
            request.setAttribute("bookInfo", bookInfo);
        } else {
            //数据库里没有这本书,或库存不足
            request.setAttribute("bookInfo", null);
        }
        return Response.createOkResp();
    }

    @DeleteMapping("/cart")
    @ApiOperation(value = "清空购物车", notes = "清空购物车")
    public ResponseResult clearCart(HttpServletRequest request) {
        cartService.clearCart(request,"cart");
        //return "cart";
        return Response.createOkResp("清空成功！");
    }

    @DeleteMapping("/cart/{bookId}")
    @ApiOperation(value = "根据id删除购物", notes = "根据id删除购物")
    public ResponseResult deleteCartItem(@PathVariable("bookId") int bookId,HttpServletRequest request){
        cartService.deleteCartItem(bookId, request);
        //return "redirect:/cart/items";
        return Response.createOkResp("删除成功！");
    }

    /**
     * 更新某个购物车项的购买数量
     * @param bookId
     * @param newNum
     * @param request
     * @return
     */
    @PutMapping("/cart")
    @ResponseBody
    @ApiOperation(value = "更新某个购物车项的购买数量", notes = "更新某个购物车项的购买数量")
    public ResponseResult updateBuyNum(int bookId, int newNum, HttpServletRequest request){
        BSResult bsResult=cartService.updateBuyNum(bookId, newNum, request);
        return Response.createResp(bsResult.getCode(), bsResult.getMessage(), bsResult.getData());
    }

    @PostMapping("/cart/checkOne")
    @ResponseBody
    @ApiOperation(value = "全选", notes = "全选")
    public ResponseResult checkACartItem(int bookId, HttpServletRequest request){
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        BSResult bsResult=cartService.checkedOrNot(cart, bookId);
        return Response.createResp(bsResult.getCode(), bsResult.getMessage(), bsResult.getData());
    }
}
